<?php
/**
 * 检查远程文件是否存在
 */
if (!function_exists('remoteFileExists')) {
    function remoteFileExists($url)
    {
        $curl = curl_init($url);
        curl_setopt($curl, CURLOPT_NOBODY, true);
        $result = curl_exec($curl);
        $is_exists = false;
        if ($result !== false) {
            $statusCode = curl_getinfo($curl, CURLINFO_HTTP_CODE);
            if ($statusCode == 200) {
                $is_exists = true;
            } elseif ($statusCode == 304) {
                $is_exists = true;
            }
        }
        curl_close($curl);
        return $is_exists;
    }
}

/**
 * 检查远程地址是否有效
 */
if (!function_exists('remoteUrlExists')) {
    function remoteUrlExists($url)
    {
        $file_headers = @get_headers($url);
        if ($file_headers === false) {
            return false;
        }
        foreach ($file_headers as $header) {
            if (preg_match("/^HTTP.+\s(\d\d\d)\s/", $header, $m)) {
                $code = $m[1];
            }
        }
        if ($code == 200) {
            return true;
        } else {
            return false;
        }
    }
}

/**
 * 获取用户目录
 */
if (!function_exists('getUserDir')) {
    function getUserDir($uid)
    {
        return 'upload/u' . base_convert(ceil($uid / 2000), 10, 36) . '/' . base_convert($uid, 10, 36) . '/';
    }
}

/**
 * 获取上传目录路径
 */
if (!function_exists('upload_path')) {
    function upload_path()
    {
        $dir = public_path() . DIRECTORY_SEPARATOR . 'upload';
        return (is_dir($dir) or mkdir($dir, 0777, true)) ? realpath($dir) : false;
    }
}

/**
 * 获取用户头像
 */
if (!function_exists('getAvatar')) {
    function getAvatar($src)
    {
        if (strlen($src)) {
            return asset($src);
        }
        return asset('assets/avatar/0.jpg');
    }
}

/**
 * 删除指定目录
 */
if (!function_exists('deleteDir')) {
    function deleteDir($dir)
    {
        $dh = opendir($dir);
        while ($file = readdir($dh)) {
            if ($file != '.' and $file != '..') {
                $fullpath = $dir . DIRECTORY_SEPARATOR . $file;
                is_dir($fullpath) ? deleteDir($fullpath) : unlink($fullpath);
            }
        }
        closedir($dh);
        return rmdir($dir);
    }
}

/**
 * 目录拷贝
 */
if (!function_exists('xCopy')) {
    function xCopy($source, $destination, $child = false)
    {
        if (!is_dir($source)) {
            return false;
        }
        if (!file_exists($destination)) {
            mkdir($destination, 0777, true);
        }

        $handle = dir($source);
        while ($entry = $handle->read()) {
            if ($entry != '.' and $entry != '..') {
                if (is_dir($source . '/' . $entry)) {
                    if ($child) {
                        xCopy($source . '/' . $entry, $destination . '/' . $entry, $child);
                    }
                } else {
                    copy($source . '/' . $entry, $destination . '/' . $entry);
                }
            }
        }
        return true;
    }
}

/**
 * 根据IP地址获取来源
 */
if (!function_exists('ipQuery')) {
    function ipQuery($ip)
    {
        if (isIp($ip) == false) {
            $ip = long2ip($ip);
        }

        $url = 'http://whois.pconline.com.cn/ip.jsp?ip=' . $ip;
        $str = @file_get_contents($url);
        if ($str !== false) {
            $str = iconv('gb2312', 'utf-8', $str);
            return $str;
        }

        $url = 'http://sucha.cwaic.com/service/query.asmx/GetIpInfo?ip=' . $ip;
        $xml = @simplexml_load_file($url);
        if ($xml !== false) {
            $data = json_decode($xml);
            $str = strtr($data[0]->Country . $data[0]->City, array('CZ88.NET' => ''));
            return $str;
        }

        $url = 'http://ipquery.sdo.com/getipinfo.php?ip=' . $ip;
        $xml = @simplexml_load_file($url);
        if ($xml !== false) {
            $str = $xml->country . $xml->city . $xml->sp;
            return $str;
        }
        return $ip . ' - Unknown Address';
    }
}
